package com.stx.user.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.stx.user.dao.UserDAO;
import com.stx.user.entity.UserEntity;
import com.stx.user.vo.UserVO;

public class UserDAOImpl extends JdbcDaoSupport implements UserDAO,
		SessionAware {

	class UserRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {

			UserEntity userEntity = new UserEntity();

			userEntity.setUserid(rs.getString("userid"));
			userEntity.setUname(rs.getString("uname"));
			userEntity.setBlogName(rs.getString("blogName"));
			userEntity.setPassWord(rs.getString("passWord"));
			userEntity.setEmail(rs.getString("email"));
			userEntity.setDescription(rs.getString("description"));
			userEntity.setLastModifyTime(rs.getString("lastModifyTime"));
			userEntity.setCreateTime(rs.getString("createTime"));

			return userEntity;
		}

	}

	private Map<String, Object> session;

	@Override
	public void setSession(Map<String, Object> session) {

		this.session = session;

	}

	@Override
	public List<UserEntity> listUser() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserEntity findUserByUesrName(String userName, String password) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql = "select * from usertable where uname = ? and password = ?";
		Object[] args = { userName, password };
		UserEntity userEntity = new UserEntity();
		try {
			userEntity = (UserEntity) jt.queryForObject(sql, args,
					new UserRowMapper());
		} catch (Exception e) {
			userEntity.setUserid(null);
		}
		return userEntity;
	}

	@Override
	public UserEntity getUserByUserName(String userName) {
		JdbcTemplate jt = getJdbcTemplate();
		String sql = "select * from usertable where uname = ?";
		Object[] args = { userName };
		UserEntity userEntity = new UserEntity();
		try {
			userEntity = (UserEntity) jt.queryForObject(sql, args,
					new UserRowMapper());
		} catch (Exception e) {
			userEntity.setUserid(null);
		}
		return userEntity;
	}

	@Override
	public UserVO getUserByUserID(String userID) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addUser(UserVO uservo) {
		// TODO Auto-generated method stub

		JdbcTemplate jt = getJdbcTemplate();
		Date date = new Date(new Date().getTime());
		String currentTime = date.getYear() + 1900 + "/"
				+ (date.getMonth() + 1) + "/" + date.getDate();
		uservo.setCreateTime(currentTime);

		String sql = "insert into usertable values(userid.nextval,?,?,?,?,?,?,?,?)";
		Object[] args = { uservo.getUname(), uservo.getBlogName(),
				uservo.getPassWord(), uservo.getEmail(),
				uservo.getDescription(), uservo.getCreateTime(),
				uservo.getLastModifyTime(), uservo.getDelflag() };
		jt.update(sql, args);
	}

	@Override
	public void delelteUser(String userID) {
		// TODO Auto-generated method stub

	}

	@Override
	public void editUser(UserVO uservo) {
		// TODO Auto-generated method stub
		JdbcTemplate jt = getJdbcTemplate();
		Date date = new Date(new Date().getTime());
		String currentTime = date.getYear() + 1900 + "/"
				+ (date.getMonth() + 1) + "/" + date.getDate();
		uservo.setLastModifyTime(currentTime);
		String sql = "update usertable set password= ?, email = ?, description = ?, lastModifyTime=? where userid = ?";
		// 构建参数数组
		Object[] args = { uservo.getPassWord(), uservo.getEmail(),
				uservo.getDescription(), uservo.getLastModifyTime(),
				uservo.getUserid() };
		jt.update(sql, args);
	}
}
